package com.wytec.xywzxw.utils.redis;

import org.springframework.util.Assert;
import redis.clients.jedis.Jedis;

import java.io.*;

public class RedisObjectSerializable {
    /**
     * 把一个对象序列化byte[] 保存到redis
     *
     * @param o     需要序列化的对象
     * @param key   保存到redis中的key值
     * @param jedis redis客户端
     * @throws IOException X
     */
    public static void serializable(Jedis jedis, Object o, String key) throws IllegalArgumentException, IOException {
        serializable(jedis, o, key,-1);
    }
    /**
     * 把一个对象序列化byte[],并设置超时时间，保存到redis
     *
     * @param jedis redis客户端
     * @param o     需要序列化的对象
     * @param key   保存到redis中的key值
     * @param expireTime   过期时间
     * @throws IOException X
     */
    public static void serializable(Jedis jedis, Object o, String key,int expireTime) throws IllegalArgumentException, IOException {
        ByteArrayOutputStream byteArrayOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            Assert.notNull(jedis, "error jedis is null");
            Assert.notNull(key, "error key is null");
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(o);
            byte[] bytes = byteArrayOutputStream.toByteArray();
            jedis.set(key.getBytes(), bytes);
            if (expireTime != -1){
                jedis.expire(key,expireTime);
            }
        } finally {
            if (objectOutputStream != null) {
                objectOutputStream.close();
            }
            if (byteArrayOutputStream != null) {
                byteArrayOutputStream.close();
            }
            if (jedis != null){
                jedis.close();
            }
        }
    }

    /**
     * 反序列化对象 把byte[]数组反序列化为Class<O> clazz指定的对象
     *
     * @param jedis jedis客户端
     * @param key   redis key值
     * @param <O>   X
     * @param clazz 需要反序列化的实体类
     * @return 反序列化后的对象
     * @throws IOException            X
     * @throws ClassNotFoundException X
     */
    public static <O> O deSerializable(Jedis jedis, Class<O> clazz, String key) throws IllegalArgumentException, IOException, ClassNotFoundException {
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            Assert.notNull(jedis, "error jedis is null");
            Assert.notNull(key, "error key is null");
            byte[] bytes = jedis.get(key.getBytes());
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return clazz.cast(objectInputStream.readObject());
        } finally {
            if (objectInputStream != null) {
                objectInputStream.close();
            }
            if (byteArrayInputStream != null) {
                byteArrayInputStream.close();
            }
            if (jedis != null){
                jedis.close();
            }
        }
    }
}
